﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using whatpricemyhouse.LINQ;

namespace whatpricemyhouse.agent
{
    public partial class BuyerList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            new AdminBasicPage();

            if (User.Identity.IsAuthenticated)
            {
                ImageButton ImageButton1 = this.LoginView2.FindControl("ImageButton1") as ImageButton;

                ImageButton1.Attributes.Add("onmouseover", "mouseOver1(this);");
                ImageButton1.Attributes.Add("onmouseout", "mouseOut1(this);");

                ImageButton ImageButton2 = this.LoginView2.FindControl("ImageButton2") as ImageButton;

                ImageButton2.Attributes.Add("onmouseover", "mouseOver2(this);");
                ImageButton2.Attributes.Add("onmouseout", "mouseOut2(this);");
                ImageButton2.Attributes.Add("OnClick", "window.print()");

                String buyerId = Request.QueryString["buyerid"];
                String command = Request.QueryString["comm"];
                if (!String.IsNullOrEmpty(command))
                {
                    if (!String.IsNullOrEmpty(buyerId))
                    {
                        if (command == "move")
                        {
                            moveOld(buyerId);
                        }
                        if (command == "delete")
                        {
                            deleteSeller(buyerId);
                        }
                    }
                }
            }
        }

        private void moveOld(string buyerId)
        {
            DataControllerDataContext dcdc = new DataControllerDataContext();
            var buyers = from b in dcdc.buy_request_tables
                         where b.BRID == buyerId
                         select b;
            foreach (var buyer in buyers) {
                buyer.isOld = false;
                break;
            }
            dcdc.SubmitChanges();

        }

        private void deleteSeller(string buyerId)
        {
            DataControllerDataContext dcdc = new DataControllerDataContext();
            var brlts = from bb in dcdc.buy_request__loan_types
                        where bb.BRID == buyerId
                        select bb;
            foreach (var brlt in brlts) {
                dcdc.buy_request__loan_types.DeleteOnSubmit(brlt);
            }


            var buyers = from bbb in dcdc.buy_request_tables
                         where bbb.BRID == buyerId
                         select bbb;
            foreach (var buyer in buyers) {
                dcdc.buy_request_tables.DeleteOnSubmit(buyer);
                break;
            }
            dcdc.SubmitChanges();
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            HyperLink hl1 = e.Item.FindControl("MoveToOld1") as HyperLink;
            HyperLink hl2 = e.Item.FindControl("Delete1") as HyperLink;
            bool isOld = false;
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["old"]))
                {
                    if (Request.QueryString["old"] == "1")
                    {
                        isOld = false;
                        hl1.Visible = true;
                        hl2.Visible = false;
                    }
                    else if (Request.QueryString["old"] == "0")
                    {
                        isOld = true;
                        hl1.Visible = false;
                        hl2.Visible = true;
                    }
                }
                else {
                    isOld = false;
                    hl1.Visible = true;
                    hl2.Visible = false;
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}